package com.kefu.robot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kefu.common.annotation.Permission;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.robot.model.RobotDocType;
import com.kefu.robot.service.RobotDocTypeService;
import com.kefu.robot.vo.RobotDocTypeInsertVo;
import com.kefu.robot.vo.RobotDocTypeSearchVo;
import com.kefu.robot.vo.RobotDocTypeUpdateVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Api(tags = "词条类型")
@RestController
@RequestMapping("robot/doctype")
public class RobotDocTypeController extends AbstractBaseController {

    @Autowired
    private RobotDocTypeService robotDocTypeService;

    @PostMapping("/add")
    @ApiOperation(value = "新增词条类型", notes = "新增词条类型")
    public Result<String> add(@Validated @RequestBody RobotDocTypeInsertVo robotDocTypeVo) {
        logger.info("新增词条类型:{},{}", KefuContext.getKefuContext(), robotDocTypeVo);
        if(!robotDocTypeVo.getParentId().equals("0") && robotDocTypeVo.getParentId().length()!=32){
            throw BusinessException.msg("上级分类不存在");
        }
        RobotDocType info = robotDocTypeVo.convert();
        info.setWeight(robotDocTypeService.getMaxWeight(KefuContext.getKefuContext().getTenantId(), robotDocTypeVo.getParentId()) + 1);
        robotDocTypeService.save(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除词条类型", notes = "页面判断如果分类下有词条，提示不能删除")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除词条类型:{},{}", KefuContext.getKefuContext(), delete);
        robotDocTypeService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改词条类型", notes = "修改词条类型")
    public Result<String> update(@Validated @RequestBody RobotDocTypeUpdateVo robotDocTypeVo) {
        logger.info("修改词条类型:{},{}", KefuContext.getKefuContext(), robotDocTypeVo);
        RobotDocType info = robotDocTypeVo.convert();
        robotDocTypeService.update(info);
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping("/list")
    @ApiOperation(value = "查询词条类型", notes = "查询词条类型")
    public Result<List<?>> list(@Validated @RequestBody RobotDocTypeSearchVo search) {
        List<?> list = robotDocTypeService.listTree(search);
        return ResultGenerator.genSuccessResult(list);
    }
}
